﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Utilities;
using KZ.Express.H.ENT;
using KZ.Express.H.Presenter;
using KZ.Express.H.BLL;
using KZ.Express.H.Settings.Zones;

namespace KZ.Express.H.Settings.Suppliers
{
    public partial class SupplierDetailView : BaseView
    {
        #region Private Variables
        List<SupplierCarrierENT> _changedRow;

        #endregion Private Variables

        #region Private Methods

        private void SupplierDetailView_Load(object sender, EventArgs e)
        {
            _changedRow = new List<SupplierCarrierENT>();
            LoadCarrier();
            LoadData();
        }

        private void LoadCarrier()
        {
            clbCarrier.DataSource = _presenter.LoadDataTable<CarrierENT>(null);
            clbCarrier.DisplayMember = "CarrierName";
            clbCarrier.ValueMember = "CarrierID";

        }

        private void LoadData()
        {
            try
            {

                switch (this.ActionName)
                {
                    case "Insert":
                        break;

                    case "Update":
                        _presenter.LoadEntity<SupplierENT>();

                        SearchCriteria search = new SearchCriteria();
                        search.Add("SupplierID", SupplierID);
                        DataTable dt = _presenter.LoadDataTable<SupplierCarrierENT>(search);
                        string listItems = String.Empty;
                        dt.AsEnumerable().ToList<DataRow>().ForEach(Obj => listItems += "," + Obj["CarrierID"].ToString());
                        listItems = listItems + ",";
                        for (int i = 0; i < clbCarrier.Items.Count; i++)
                        {
                            DataRowView view = clbCarrier.Items[i] as DataRowView;
                            if (listItems.Contains("," + view["CarrierID"] + ","))
                                clbCarrier.SetItemChecked(i, true);
                            else
                                clbCarrier.SetItemChecked(i, false);
                        }
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }


        #endregion Private Methods

        #region Constructors

        public SupplierDetailView()
        {
            InitializeComponent();

            _presenter = new BasePresenter(this, new SupplierBLL());
            this.ObjectID = (int)Objects.Supplier;
        }

        #endregion Constructors

        #region Public Properties

        [Search]
        public int SupplierID
        {
            get { return base.EntityID; }
        }

        public override BaseENT Entity
        {
            get
            {
                SupplierENT supplier = new SupplierENT();
                supplier.SupplierID = base.EntityID;
                supplier.SupplierName = txtSupplierName.Text;
                supplier.CreatedBy = UserENT._USERID;
                supplier.UpdatedBy = UserENT._USERID;

                supplier.SuppliersCarriers = _changedRow;
                supplier.ValidateData();

                return supplier;
            }
            set
            {
                SupplierENT user = value == null ? new SupplierENT() : value as SupplierENT;
                txtSupplierName.Text = user.SupplierName;

            }
        }

        #endregion Public Properties

        #region Public Methods

        public override void Save()
        {
            try
            {

                foreach (object item in clbCarrier.CheckedItems)
                {
                    DataRowView view = item as DataRowView;
                    _changedRow.Add(new SupplierCarrierENT()
                    {
                        SupplierID = this.EntityID,
                        CarrierID = Utility.IntParse(view["CarrierID"]),
                        CreatedBy = UserENT._USERID,
                        UpdatedBy = UserENT._USERID
                    });
                }
                _presenter.Save();
                _changedRow.Clear();
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        #endregion Public Methods





    }
}
